package com.xy.blog.capacity.remote;

import com.xy.blog.article.api.ArticleCategoryService;
import com.xy.blog.article.api.ArticlePicService;
import com.xy.blog.article.api.ArticleService;
import com.xy.blog.article.api.SpecialColumnService;
import com.xy.blog.article.api.model.ArticleQuery;
import com.xy.blog.article.api.model.SpecialColumnQuery;
import com.xy.blog.core.springmvc.result.page.PageReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/7/14
 */
@Component
public class ArticleCenterService {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private ArticlePicService articlePicService;

    @Autowired
    private SpecialColumnService specialColumnService;


    public Object specialColumnList(PageReq<SpecialColumnQuery> req) {
        return specialColumnService.pages(req);
    }

    public Object articleHomePage(PageReq<ArticleQuery> req){
        ArticleQuery params = req.getParams();
        if (params != null) {
            Long specialColumnId = params.getSpecialColumnId();
            if (specialColumnId != null) {
                this.specialColumnViewCount(specialColumnId);
            }
        }
        return articleService.pages(req);
    }

    @Async
    public void specialColumnViewCount(Long id) {
        try {
            specialColumnService.view(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    public Object detail(Long id) {
        this.viewCount(id);
        return articleService.getById(id);
    }

    /**
     * 浏览量统计
     * @param id
     */
    @Async
    public void viewCount(Long id) {
        articleService.view(id);
    }

    public Object getCategory() {
        return articleCategoryService.listWithChildren(2L);
    }

    public Object like(Long id) {
        return articleService.like(id);
    }

    public Object getPic(Long id) {
        return articlePicService.download(id);
    }

}
